package com.da.springtest.util;

import java.util.Properties;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

public class PropertiesUtil {

    /**
     * 通过配置文件名读取内容
     *
     * @param fileName
     * @return
     */
    public static Properties readPropertiesFile(String fileName) {
        try {
            Resource resource = new ClassPathResource(fileName);
            Properties props = PropertiesLoaderUtils.loadProperties(resource);
            return props;
        } catch (Exception e) {
            System.out.println("————读取配置文件：" + fileName + "出现异常，读取失败————");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 读取默认配置文件
     */
    public static Properties readPropertiesFile() {
        try {
            Resource resource = new ClassPathResource("application.properties");
            Properties props = PropertiesLoaderUtils.loadProperties(resource);
            return props;
        } catch (Exception e) {
            System.out.println("————读取配置文件：application.properties出现异常，读取失败————");
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        Properties properties = readPropertiesFile("application.properties");
        System.out.println(properties.getProperty("env"));
    }
}